<?

require_once("cms/core/helper/Route.php");
require_once("cms/menu/om/MenuPeer.php");

class PHP_CMS_View_Helper_MenuRoute extends PHP_CMS_View_Helper_Route {

    public function menuRoute($parentId) {

        $route = array();

        if($parentId > 0) {

            while($parentId > 0) {

                $sql = "SELECT menu_item_i18n.name, menu_item.parent_id FROM menu_item_i18n " .
                       "LEFT JOIN menu_item USING (menu_item_id) " .
                       "LEFT JOIN menu USING (menu_id) " .
                       "WHERE menu_item_i18n.language_code = menu.language_code " .
                       "AND menu_item_i18n.menu_item_id = ". $parentId . " " .
                       "LIMIT 1";

                $conn = Propel::getConnection();
                $stmt = $conn->createStatement();
                $rs   = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
//
//                $c = new Criteria();
//                $c->clearSelectColumns();
//                $c->addAlias("join2", MenuPeer::TABLE_NAME);
//
//                $c->addSelectColumn(MenuItemI18nPeer::NAME);
//                $c->addSelectColumn(MenuItemPeer::PARENT_ID);
//
//                $c->addJoin(MenuItemI18nPeer::MENU_ITEM_ID,
//                    MenuItemPeer::MENU_ITEM_ID, Criteria::LEFT_JOIN);
//
////                $c->addJoin(MenuItemPeer::MENU_ID,
////                    MenuPeer::MENU_ID, Criteria::LEFT_JOIN);
//
//                $c->addJoin(MenuItemI18nPeer::LANGUAGE_CODE,
//                    "join2.language_code", Criteria::LEFT_JOIN);
//
//                $c->add(MenuItemI18nPeer::MENU_ITEM_ID,
//                    $parentId);
//
//                $c->setLimit(1);

//                $rs = MenuItemI18nPeer::doSelectRs($c);

                if($rs->next()) {
                    $parentId = $rs->getInt(2) === null ?
                        0 : $rs->getInt(2);
                    $route[] = array(
                        "name"=>$rs->getString(1),
                        "href"=>"menu/item/list?parentId=" . $parentId
                    );
                } else {
                    $parentId = 0;
                }

            }

//            $route = array_slice($route, 1);
            $route = array_reverse($route);

        }

        return $route;


    }

}

?>